package ar.com.coonocer.joy.eclipseplugin.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import ar.com.coonocer.joy.eclipseplugin.pages.PluginPropertyPage;
import ar.com.coonocer.joy.mdg.util.Generator;

public class TransformAction implements IObjectActionDelegate {

	private static final String DIALOG_TITLE = "J-OY Plugin";

	private Shell shell;
	private ISelection selection;

	public TransformAction() {
		super();
	}

	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	public void run(IAction action) {
		try {
			process(action);
		} catch (Throwable e) {
			MessageDialog.openInformation(shell, DIALOG_TITLE, "Transformation error:" + e);
		}
	}

	private void process(IAction action) {
		IFile file = (IFile) ((IStructuredSelection) selection).getFirstElement();
		File xmiFile = file.getLocation().toFile();
		//
		File destinationFolder;
		try {
			destinationFolder = new File(file.getProject().getPersistentProperty(new QualifiedName("", PluginPropertyPage.PATH_OUTPUT_PROPERTY)));
			//
			if (!destinationFolder.exists()) {
				MessageDialog.openInformation(shell, DIALOG_TITLE, "Output folder does not exist. Check project properties.");
				return;
			}
		} catch (CoreException e) {
			MessageDialog.openInformation(shell, DIALOG_TITLE, "Unable to retrieve output folder property.");
			return;
		}
		//
		File templateFolder;
		try {
			templateFolder = new File(file.getProject().getPersistentProperty(new QualifiedName("", PluginPropertyPage.PATH_TEMPLATES_PROPERTY)));
			if (!templateFolder.exists()) {
				MessageDialog.openInformation(shell, DIALOG_TITLE, "Template folder does not exist. Check project properties.");
				return;
			}
			//
			templateFolder = new File(templateFolder, file.getProject().getPersistentProperty(new QualifiedName("", PluginPropertyPage.TEMPLATE_GROUP_PROPERTY)));
			if (!templateFolder.exists()) {
				MessageDialog.openInformation(shell, DIALOG_TITLE, "Template group does not exist. Check project properties.");
				return;
			}
			//
			templateFolder = new File(templateFolder, file.getProject().getPersistentProperty(new QualifiedName("", PluginPropertyPage.TEMPLATE_NAME_PROPERTY)));
			if (!templateFolder.exists()) {
				MessageDialog.openInformation(shell, DIALOG_TITLE, "Template name does not exist. Check project properties.");
				return;
			}
		} catch (CoreException e) {
			MessageDialog.openInformation(shell, DIALOG_TITLE, "Unable to retrieve template folder property.");
			return;
		}
		//
		Properties properties = new Properties();
		String propertiesFileName = "/j-oy.properties";
		try {
			properties.load(new FileInputStream(propertiesFileName));
		} catch (FileNotFoundException e) {
			throw new RuntimeException("Unable to load properties file: " + propertiesFileName, e);
		} catch (IOException e) {
			throw new RuntimeException("Unable to load properties file: " + propertiesFileName, e);
		}
		//
		Generator.generate(xmiFile, templateFolder, destinationFolder, properties);
		//
		MessageDialog.openInformation(shell, DIALOG_TITLE, "Transformation was executed.");
	}

	public void selectionChanged(IAction action, ISelection selection) {
		this.selection = selection;
	}

}
